<?php

/**
 * @author zhuludong
 * @copyright 2018.8.3
 */
namespace Monopolygame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

class LoginController extends ApiController {
    
    const GAME_TYPE = 'Monopolygame';  // 游戏类型

//  const MONOLY_AUTH = "monolys:auth"; // 游戏授权用户改为游戏次数

    const MONOLY_TOURIST = "monolys:tourist"; // 游戏游客用户

    const MONOLY_AUTH_USER = "monolys:auth_user";  // 授权用户
    
    // 登陆授权接口
    public function login()
    {
        $gameType = self::GAME_TYPE;
        $id = I('id','','int');
        $openid = I('openid','','string');
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');
        $code = I('code','','string');
        if($code) {
            $info = $this->wxAuthor($gameType,$code);
            //$this->response(1,'code不能为空');
        }
        $openids = !empty($openid) ? $openid : $info['openid'];

        /*if(empty($id) || empty($openid) || empty($nickname) || empty($avatar)){
            $this->response(2,'参数不能为空');
        }*/

        $userInfo['openid'] = $openids;
        $userInfo['nickname'] = $nickname;
        $userInfo['avatar'] = $avatar;
        $userInfo['id'] = $id;

        $ret = D('users')->createUser($userInfo); // 创建用户

        $this->countLog('monolys',self::MONOLY_AUTH_USER);  // 授权用户
        log::write("login:".print_r($ret,true)."=======".date('Y-m-d H:i:s'),"ERROR");
        $this->response(0,'ok',$ret);

    }

    //openid,sessionkey
    public function loginNotAuth() {
        $id = I('id','','string');
        $code = I('code','','string');
        if(!$code) {
            $this->response(1,'code不能为空');
        }   
        $gameType = self::GAME_TYPE;
        $info = $this->wxAuthor($gameType,$code);

        $data['openid'] = $info['openid'];

        if(!empty($info['openid'])) {
            $content['openid'] = $info['openid'];
        }else {
            $content['id'] = $id;
        }

        $userInfo = M('users')->where($content)->find();

        if(empty($userInfo)) {
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['types'] = 1;//未授权新用户
            $ret = M('users')->add($data);
            if(empty($ret)) {
                $this->response(1,'操作失败');
            }

            $map['id'] = $ret;
            $userInfo = M('users')->where($map)->find();
            $userInfo['session_key'] = $info['session_key'];
        }else{
            if($userInfo['types'] != 4){
                if($userInfo['status']!=2){
                    //授权第一次登录
                    $data['types'] = 2;
                }else{
                    // 授权登录
                    if(!empty($userInfo['nickname']) && !empty($userInfo['avatar'])){
                        $data['types'] = 4;
                    }else{
                        $data['types'] = 3;
                    }
                }

                $data['update_time'] = date('Y-m-d H:i:s');
                $ret = M('users')->where($content)->save($data);
                if(empty($ret)) {
                    $this->response(1,'操作失败');
                }
            }

            $userInfo = M('users')->where($content)->find();
            $userInfo['session_key'] = $info['session_key'];
        }
        /*$this->countLog('monolys',self::MONOLY_TOURIST);  // 游客用户
        $this->response(0,'ok',$userInfo);*/
        // $this->countLog('monolys',self::MONOLY_AUTH);  // 记录用户进入游戏次数
        $userInfo['session_key'] = $info['session_key'];
        $this->response(0,'ok',$userInfo);

    }

    /**
     * 获取sessionID
     */
    public function getSessionId() {
       
        $id = I('id',0,'int'); // 用户编号

        if(!empty($id)) {
            session('monoly:uid',$id);
            $sessionId = session_id();
            $this->ajaxReturn(array('PHPSESSID'=>$sessionId));
        }
        $this->response(0,'ok');
        
    }
}